In [1]:
# Generate pentagonal numbers
# p(k) = k * (3k-1) / 2, k not zero.
def pentagonal(N):
a, b = 1, 2
delta = 4
sgn = 1
while a <= N:
yield sgn, a
a += delta
if b <= N:
yield sgn, b
b += delta + 1
delta += 3
sgn = -sgn
N = 100
partitions = [0] * (N+1)
partitions[0] = 1
for n in range(1, N+1):
for sgn, g in pentagonal(n):
partitions[n] += sgn * partitions[n - g]
print(partitions[N])
Explanation: Uses the pentagonal number recurrence for the partition counting function.
http://en.wikipedia.org/wiki/Pentagonal_number_theorem#Partition_recurrence